﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YouDaoNotesCodes.List
{
    /// <summary>
    /// 雇员类
    /// </summary>
    public class Employee
    {
        /// <summary>
        /// 雇员姓名
        /// </summary>
        public string EmpName { get; set; }
        /// <summary>
        /// 雇员性别
        /// </summary>
        public string EmpSex { get; set; }
        /// <summary>
        /// 雇员年龄
        /// </summary>
        public int EmpAge { get; set; }
        /// <summary>
        /// 雇员部门
        /// </summary>
        public string DeptName { get; set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="empName"></param>
        /// <param name="empSex"></param>
        /// <param name="empAge"></param>
        /// <param name="deptName"></param>
        public Employee(string empName, string empSex, int empAge, string deptName)
        {
            EmpName = empName;
            EmpSex = empSex;
            EmpAge = empAge;
            DeptName = deptName;
        }

        public override string ToString()
        {
            return "Employee[EmpName=" + EmpName + ",EmpSex=" + EmpSex + ",EmpAge=" + EmpAge + ",DeptName=" + DeptName + "]";
        }
    }
    class ListMethods
    {
        public void ListMethodsApplication()
        {
            List<Employee> employees = new List<Employee>();
            Employee empOne = new Employee("王晶", "男", 20, "市场部");
            Employee empTwo = new Employee("陈浩民", "男", 24, "技术部");
            Employee empThree = new Employee("王诗玲", "女", 25, "市场部");
            Employee empFour = new Employee("陈绍聪", "男", 30, "技术部");
            Employee empFive = new Employee("张倩", "女", 19, "行政部");
            employees.Add(empOne);
            employees.Add(empTwo);
            employees.Add(empThree);
            employees.Add(empFour);
            employees.Add(empFive);

            List<Employee> listEmps = new List<Employee>();
            //① ForEach(Action<T> action) 对 System.Collections.Generic.List`1 的每个元素执行指定操作
            employees.ForEach(p =>
            {
                if (p.EmpSex.Equals("女"))
                {
                    listEmps.Add(p);
                }
            });
            if (listEmps.Count > 0)
            {
                foreach (Employee emp in listEmps)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //② Where 基于谓词筛选值序列
            List<Employee> listEmployee = employees.Where(p => (p.EmpName.Contains("王"))).ToList();
            //var whereRemove = employees.FirstOrDefault(t => t.EmpName == "王");

            if (listEmployee.Count > 0)
            {
                foreach (Employee emp in listEmployee)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //区分FirstOrDefault和Where
            //使如何移除数组中的id号为001
            string res = "[{\"id\":\"001\",\"name\":\"zwq\"},{\"id\":\"002\",\"name\":\"cnm\"},{\"id\":\"001\",\"name\":\"mls\"}]";

            List<pp> list = JsonConvert.DeserializeObject<List<pp>>(res);
            List<pp> li = new List<pp>();
            int count = list.Count;
            //for (int i = 0; i < list.Count; i++) //错误写法，for内部每次回remove，Conut是个不定值
            for (int i = 0; i < count; i++)
            {
                var whereRemove = list.FirstOrDefault(t => t.id == "001");//注意：返回满足条件中的第一个元素
                list.Remove(whereRemove);

            }
            var ps = list.Where(t => t.id == "001").ToList();//注意：返回的是序列需要遍历移除
            //for (int j = 0; j < list.Count; j++)//错误写法，for内部每次回remove，Conut是个不定值
            for (int j = 0; j < count; j++)
            {
                list.Remove(ps[j]);
            }

            //③ FindAll(Predicate<T> match) 检索与指定谓词定义的条件匹配的所有元素
            List<Employee> empList = employees.FindAll(p => (p.EmpAge > 22));
            if (empList.Count > 0)
            {
                foreach (Employee emp in empList)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //④ RemoveAll(Predicate<T> match) 移除与指定的谓词所定义的条件相匹配的所有元素
            employees.RemoveAll(p => (p.EmpAge >= 25));
            if (employees.Count > 0)
            {
                foreach (Employee emp in employees)
                {
                    Console.WriteLine(emp.ToString());
                }
            }
            Console.WriteLine("=========================");

            //⑤ RemoveAt(int index) 移除指定索引处的元素
            if (employees.Count > 0)
            {
                for (int i = 0; i < employees.Count; i++)
                {
                    Employee employee = employees[i];
                    if (employee.DeptName.Equals("市场部"))
                    {
                        employees.RemoveAt(i);
                    }
                }
                if (employees.Count > 0)
                {
                    foreach (Employee emp in employees)
                    {
                        Console.WriteLine(emp.ToString());
                    }
                }
            }
            //⑥ OrderByDescending和OrderBy
            List<Employee> empSortList = employees;//假如给年龄排序
            empSortList = empSortList.OrderByDescending(p => p.EmpAge).ToList();//order by EmpSex desc
            empSortList = empSortList.OrderBy(p => p.EmpAge).ToList();//order by EmpSex asc
            foreach (Employee ep in empSortList)
            {
                Console.WriteLine(ep.EmpAge);
            }

        }
    }
    class pp
    {
        public string id { get; set; }
        public string name { get; set; }
    }
    public class MyDictionaryComparer : IEqualityComparer<string>
    {
        public bool Equals(string x, string y)
        {
            //throw new NotImplementedException();
            return x != y;
        }

        public int GetHashCode(string obj)
        {
            //throw new NotImplementedException();
            return obj.GetHashCode();
        }
    }
}
